home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / indent.lha / indent / parse.c < prev    next >
C/C++ Source or Header  |  1992-07-06  |  11KB  |  339 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that the above copyright notice and this paragraph are
  9.  * duplicated in all such forms and that any documentation,
  10.  * advertising materials, and other materials related to such
  11.  * distribution and use acknowledge that the software was developed
  12.  * by the University of California, Berkeley, the University of Illinois,
  13.  * Urbana, and Sun Microsystems, Inc.  The name of either University
  14.  * or Sun Microsystems may not be used to endorse or promote products
  15.  * derived from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  */
  20.  
  21. #ifndef lint
  22. # ifndef OS2
  23.     static char sccsid[] = "@(#)parse.c    5.8 (Berkeley) 92/06/15";
  24. # endif
  25. #endif                          /* not lint */
  26.  
  27. #include "globals.h"
  28. #include "codes.h"
  29.  
  30. #ifdef ANSIC
  31. static void reduce(void);
  32.  
  33. void parse(int tk)
  34. #else
  35. parse(tk)
  36.     int tk;                     /* the code for the construct scanned */
  37.  
  38. #endif
  39. {
  40.     int i;
  41.  
  42. #ifdef debug
  43.     printf("%2d - %s\n", tk, token);
  44. #endif
  45.  
  46.     while (ps.p_stack[ps.tos] == ifhead && tk != elselit)
  47.     {
  48.         /* true if we have an if without an else */
  49.         ps.p_stack[ps.tos] = stmt;  /* apply the if(..) stmt ::= stmt reduction */
  50.         reduce();               /* see if this allows any reduction */
  51.     }
  52.  
  53.  
  54.     switch (tk)
  55.     {                           /* go on and figure out what to do with the
  56.                                    input */
  57.  
  58.     case decl:                 /* scanned a declaration word */
  59.         ps.search_brace = btype_2 && !btype_3;
  60.         /* indicate that following brace should be on same line */
  61.         if (ps.p_stack[ps.tos] != decl)
  62.         {                       /* only put one declaration onto stack */
  63.             break_comma = true; /* while in declaration, newline should be
  64.                                    forced after comma */
  65.             ps.p_stack[++ps.tos] = decl;
  66.             ps.il[ps.tos] = ps.i_l_follow;
  67.  
  68.             if (ps.ljust_decl)
  69.             {                   /* only do if we want left justified
  70.                                    declarations */
  71.                 ps.ind_level = 0;
  72.                 for (i = ps.tos - 1; i > 0; --i)
  73.                     if (ps.p_stack[i] == decl)
  74.                         ++ps.ind_level; /* indentation is number of declaration
  75.                                            levels deep we are */
  76.                 ps.i_l_follow = ps.ind_level;
  77.             }
  78.         }
  79.         break;
  80.  
  81.     case ifstmt:               /* scanned if (...) */
  82.         if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
  83.             ps.i_l_follow = ps.il[ps.tos];
  84.     case dolit:                /* 'do' */
  85.     case forstmt:              /* for (...) */
  86.         ps.p_stack[++ps.tos] = tk;
  87.         ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
  88.         ++ps.i_l_follow;        /* subsequent statements should be indented 1 */
  89.         ps.search_brace = btype_2 && !btype_3;
  90.         break;
  91.  
  92.     case lbrace:               /* scanned { */
  93.         break_comma = false;    /* don't break comma in an initial list */
  94.         if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
  95.             || ps.p_stack[ps.tos] == stmtl)
  96.             ++ps.i_l_follow;    /* it is a random, isolated stmt group or a
  97.                                    declaration */
  98.         else
  99.         {
  100.             if (s_code == e_code)
  101.             {
  102.                 /*
  103.                    only do this if there is nothing on the line
  104.                 */
  105.                 --ps.ind_level;
  106.                 /*
  107.                    it is a group as part of a while, for, etc.
  108.                 */
  109.                 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= (float) 1 &&
  110.                     ps.case_code_indent >= (float) 1)
  111.                     --ps.ind_level;
  112.                 /*
  113.                    for a switch, brace should be two levels out from the code
  114.                 */
  115.             }
  116.         }
  117.  
  118.         ps.p_stack[++ps.tos] = lbrace;
  119.         ps.il[ps.tos] = ps.ind_level;
  120.         ps.p_stack[++ps.tos] = stmt;
  121.         /* allow null stmt between braces */
  122.         ps.il[ps.tos] = ps.i_l_follow;
  123.         break;
  124.  
  125.     case whilestmt:            /* scanned while (...) */
  126.         if (ps.p_stack[ps.tos] == dohead)
  127.         {
  128.             /* it is matched with do stmt */
  129.             ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
  130.             ps.p_stack[++ps.tos] = whilestmt;
  131.             ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
  132.         }
  133.         else
  134.         {                       /* it is a while loop */
  135.             ps.p_stack[++ps.tos] = whilestmt;
  136.             ps.il[ps.tos] = ps.i_l_follow;
  137.             ++ps.i_l_follow;
  138.             ps.search_brace = btype_2 && !btype_3;
  139.         }
  140.  
  141.         break;
  142.  
  143.     case elselit:              /* scanned an else */
  144.  
  145.         if (ps.p_stack[ps.tos] != ifhead)
  146.             diag(1, "Unmatched 'else'");
  147.         else
  148.         {
  149.             ps.ind_level = ps.il[ps.tos]; /* indentation for else should be
  150.                                              same as for if */
  151.             ps.i_l_follow = ps.ind_level + 1; /* everything following should be
  152.                                                  in 1 level */
  153.             ps.p_stack[ps.tos] = elsehead;
  154.             /* remember if with else */
  155.             ps.search_brace = (btype_2 && !btype_3) | ps.else_if;
  156.         }
  157.         break;
  158.  
  159.     case rbrace:               /* scanned a } */
  160.         /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
  161.         if (ps.p_stack[ps.tos - 1] == lbrace)
  162.         {
  163.             if (btype_3)
  164.                 ps.i_l_follow = ps.il[--ps.tos];
  165.             else
  166.                 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
  167.             ps.p_stack[ps.tos] = stmt;
  168.         }
  169.         else
  170.             diag(1, "Stmt nesting error.");
  171.         break;
  172.  
  173.     case swstmt:               /* had switch (...) */
  174.         ps.p_stack[++ps.tos] = swstmt;
  175.         ps.cstk[ps.tos] = case_ind;
  176.         /* save current case indent level */
  177.         ps.il[ps.tos] = ps.i_l_follow;
  178.         case_ind = ps.i_l_follow + ps.case_indent;
  179.         /*
  180.            cases should be one level down from switch
  181.         */
  182.         ps.i_l_follow += ps.case_indent + ps.case_code_indent;
  183.         /*
  184.            statements should be two levels in
  185.         */
  186.         ps.search_brace = btype_2 && !btype_3;
  187.         break;
  188.  
  189.     case semicolon:            /* this indicates a simple stmt */
  190.         break_comma = false;    /* turn off flag to break after commas in a
  191.                                    declaration */
  192.         ps.p_stack[++ps.tos] = stmt;
  193.         ps.il[ps.tos] = ps.ind_level;
  194.         break;
  195.  
  196.     default:                   /* this is an error */
  197.         diag(1, "Unknown code to parser");
  198.         return;
  199.  
  200.  
  201.     }                           /* end of switch */
  202.  
  203.     reduce();                   /* see if any reduction can be done */
  204.  
  205. #ifdef debug
  206.     for (i = 1; i <= ps.tos; ++i)
  207.         printf("(%d %d)", ps.p_stack[i], ps.il[i]);
  208.     printf("\n");
  209. #endif
  210.  
  211.     return;
  212. }
  213.  
  214. /*
  215.  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
  216.  *
  217.  * All rights reserved
  218.  *
  219.  *
  220.  * NAME: reduce
  221.  *
  222.  * FUNCTION: Implements the reduce part of the parsing algorithm
  223.  *
  224.  * ALGORITHM: The following reductions are done.  Reductions are repeated until
  225.  * no more are possible.
  226.  *
  227.  * Old TOS        New TOS <stmt> <stmt>    <stmtl> <stmtl> <stmt>    <stmtl> do
  228.  * <stmt>    "dostmt" if <stmt>    "ifstmt" switch <stmt>    <stmt> decl
  229.  * <stmt>    <stmt> "ifelse" <stmt>    <stmt> for <stmt>    <stmt> while
  230.  * <stmt>    <stmt> "dostmt" while    <stmt>
  231.  *
  232.  * On each reduction, ps.i_l_follow (the indentation for the following line) is
  233.  * set to the indentation level associated with the old TOS.
  234.  *
  235.  * PARAMETERS: None
  236.  *
  237.  * RETURNS: Nothing
  238.  *
  239.  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
  240.  *
  241.  * CALLS: None
  242.  *
  243.  * CALLED BY: parse
  244.  *
  245.  * HISTORY: initial coding     November 1976    D A Willcox of CAC
  246.  *
  247.  */
  248. /*----------------------------------------------*\
  249. |   REDUCTION PHASE                    |
  250. \*----------------------------------------------*/
  251. #ifdef ANSIC
  252. static void reduce(void)
  253. #else
  254. reduce()
  255. #endif
  256. {
  257.  
  258.     register int i;
  259.  
  260.     for (;;)
  261.     {                           /* keep looping until there is nothing left to
  262.                                    reduce */
  263.  
  264.         switch (ps.p_stack[ps.tos])
  265.         {
  266.  
  267.         case stmt:
  268.             switch (ps.p_stack[ps.tos - 1])
  269.             {
  270.  
  271.             case stmt:
  272.             case stmtl:
  273.                 /* stmtl stmt or stmt stmt */
  274.                 ps.p_stack[--ps.tos] = stmtl;
  275.                 break;
  276.  
  277.             case dolit:        /* <do> <stmt> */
  278.                 ps.p_stack[--ps.tos] = dohead;
  279.                 ps.i_l_follow = ps.il[ps.tos];
  280.                 break;
  281.  
  282.             case ifstmt:
  283.                 /* <if> <stmt> */
  284.                 ps.p_stack[--ps.tos] = ifhead;
  285.                 for (i = ps.tos - 1;
  286.                      (
  287.                       ps.p_stack[i] != stmt
  288.                       &&
  289.                       ps.p_stack[i] != stmtl
  290.                       &&
  291.                       ps.p_stack[i] != lbrace
  292.                       );
  293.                      --i);
  294.                 ps.i_l_follow = ps.il[i];
  295.                 /*
  296.                    for the time being, we will assume that there is no else on
  297.                    this if, and set the indentation level accordingly. If an
  298.                    else is scanned, it will be fixed up later
  299.                 */
  300.                 break;
  301.  
  302.             case swstmt:
  303.                 /* <switch> <stmt> */
  304.                 case_ind = ps.cstk[ps.tos - 1];
  305.  
  306.             case decl:         /* finish of a declaration */
  307.             case elsehead:
  308.                 /* <<if> <stmt> else> <stmt> */
  309.             case forstmt:
  310.                 /* <for> <stmt> */
  311.             case whilestmt:
  312.                 /* <while> <stmt> */
  313.                 ps.p_stack[--ps.tos] = stmt;
  314.                 ps.i_l_follow = ps.il[ps.tos];
  315.                 break;
  316.  
  317.             default:           /* <anything else> <stmt> */
  318.                 return;
  319.  
  320.             }                   /* end of section for <stmt> on top of stack */
  321.             break;
  322.  
  323.         case whilestmt:        /* while (...) on top */
  324.             if (ps.p_stack[ps.tos - 1] == dohead)
  325.             {
  326.                 /* it is termination of a do while */
  327.                 ps.p_stack[--ps.tos] = stmt;
  328.                 break;
  329.             }
  330.             else
  331.                 return;
  332.  
  333.         default:               /* anything else on top */
  334.             return;
  335.  
  336.         }
  337.     }
  338. }
  339.